#include"include/lyrictool.h"
#include<taglib/fileref.h>
#include<taglib/tag.h>

QString LyricTool::lyricFileName(const QString &trackName)
{
    QSettings settings("HJF","Minip");
    QString lyricPath=settings.value("LyricPath",QDir::homePath().append("/Minip/lyrics")).toString();
    lyricPath.append("/");
    TagLib::FileRef f(trackName.toStdString().c_str());
    TagLib::Tag *tag=0;
    tag=f.tag();
    if(tag)
    {
        TagLib::String as=tag->artist();
        TagLib::String ts=tag->title();
        QString fileName(as.toCString(true));
        if(!fileName.isEmpty())
            fileName.append("-");
        fileName.append(ts.toCString(true));
        if(!fileName.isEmpty())
        {
            fileName.append(".lrc");
            fileName.prepend(lyricPath);
            return fileName;         // return a possible name of lyrics file
        }
    }
    QFileInfo file(trackName);
    QString name(file.fileName());
    name.prepend(lyricPath);
    int idx=name.lastIndexOf(".");
    return name.left(idx).append(".lrc");       //return a return a possible name of lyrics file
}

QStringList LyricTool::trackInfo(const QString &trackName)
{
    QStringList info;
    TagLib::FileRef f(trackName.toStdString().c_str());
    TagLib::Tag *tag=0;
    tag=f.tag();
    if(tag)
    {
        TagLib::String as=tag->artist();
        TagLib::String ti=tag->title();
        TagLib::String al=tag->album();
        info.append(QString(as.toCString(true)));
        info.append(QString(ti.toCString(true)));
        info.append(QString(al.toCString(true)));
    }
    return info;
}

QString LyricTool::trackTitle(const QString &trackName)
{
    QString title;
    TagLib::FileRef f(trackName.toStdString().c_str());
    TagLib::Tag *tag=0;
    tag=f.tag();
    if(tag)
    {
         TagLib::String ti=tag->title();
         title.append(QString(ti.toCString(true)));
    }
    return title;
}

QString LyricTool::trackAlbum(const QString &trackName)
{
    QString album;
    TagLib::FileRef f(trackName.toStdString().c_str());
    TagLib::Tag *tag=0;
    tag=f.tag();
    if(tag)
    {
         TagLib::String al=tag->album();
         album.append(QString(al.toCString(true)));
    }
    return album;
}

QString LyricTool::trackArtist(const QString &trackName)
{
    QString artist;
    TagLib::FileRef f(trackName.toStdString().c_str());
    TagLib::Tag *tag=0;
    tag=f.tag();
    if(tag)
    {
         TagLib::String ar=tag->artist();
         artist.append(QString(ar.toCString(true)));
    }
    return artist;
}
